package BitManipulation;

import java.util.Arrays;

public class _137_SingleNumberII {
    //solution 1:sue sort method
    public int _0(int[] nums) {
        Arrays.sort(nums);
        for (int i = 0; i < nums.length-1; i+=3) {
            if (nums[i] != nums[i + 1]) {
                return nums[i];
            }
        }
        return nums[nums.length - 1];
    }

    //reference solution 2:use bit manipulation
    //hint:if the number appear once,them one will be the number and two equals 0,if twice,one will be 0 and two equals the number,
    //when it appears three times,one will disappear the numbers's influence.
    public int singleNumber(int[] nums) {
       int one = 0,two = 0;
        for (int n : nums) {
            one = (one ^ n) & ~two;
            two = (two ^ n) & ~one;
        }
        return one;
    }
}
